package cn.etgps.etgps.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

import cn.etgps.etgps.R;
import cn.etgps.etgps.base.BaseActivity;
import cn.etgps.etgps.blmanager.UserManager;
import cn.etgps.etgps.view.TitleBar;

/**
 * 这是个人资料的类 包括了“关于” “升级” “密码修改” “已经反馈”等
 */
public class MeActivity extends BaseActivity implements View.OnClickListener {
    private TitleBar mTitlebarView = null;
    private UserManager mUserManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me);
        initView();
    }

    private void initView() {
        mTitlebarView = (TitleBar) findViewById(R.id.title_bar);
        mTitlebarView.initTitleBarInfo(getString(R.string.label_me_center), "", R.drawable.icon_return, "", 0, false);
        mTitlebarView.setMainClickListener(this, getString(R.string.btn_back), "");
        mUserManager = new UserManager(this);
        Button quitButton = (Button) findViewById(R.id.quitApp);

        View resetPasswordView = findViewById(R.id.reset_password_item);
        resetPasswordView.setOnClickListener(this);
        View aboutView = findViewById(R.id.about_item);
        aboutView.setOnClickListener(this);
        View feedbackView = findViewById(R.id.feedback_item);
        feedbackView.setOnClickListener(this);
        View checkUpdateView = findViewById(R.id.check_update_item);
        checkUpdateView.setOnClickListener(this);
        quitButton.setOnClickListener(this);
        View trafficLogView = findViewById(R.id.traffic_log_item);
        trafficLogView.setOnClickListener(this);
    }

    @Override
    public void clickButtonListener(String name) {
        if (name.equals(getString(R.string.btn_back))) {
            this.finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.traffic_log_item:
                startActivity(new Intent(this, TrafficActivity.class));
                break;
            case R.id.reset_password_item:
                Intent pass = new Intent(this, UpdatePassWordActivity.class);
                startActivity(pass);
                break;
            case R.id.about_item:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.check_update_item:
                UmengUpdateAgent.setUpdateCheckConfig(false);
                UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
                    @Override
                    public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
                        if (updateStatus == UpdateStatus.No) {
                            mUserManager.showToastMessage(getString(R.string.no_update_version), 2000);
                        }
                    }
                });
                if (mUserManager.checkNetwork(this)) {
                    UmengUpdateAgent.setUpdateCheckConfig(false);
                    UmengUpdateAgent.setUpdateUIStyle(UpdateStatus.STYLE_DIALOG);
                    UmengUpdateAgent.forceUpdate(this.getApplicationContext());
                } else {
                    mUserManager.showToastMessage(getString(R.string.toast_no_network), 1000);
                }
                break;
            case R.id.feedback_item:
                startActivity(new Intent(this, FeedBackActivity.class));

                break;
            case R.id.quitApp:
                startActivity(new Intent(this, LoginActivity.class));
                this.finish();
                super.clickButtonListener("Logout");
                break;
            default:
                break;
        }
    }
}
